package interviewpreparationkit.dictionary

import org.junit.jupiter.api.Test
import redirectSystemIn
import redirectSystemOut
import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.*

class FreqQueryTests {
    @Test
    fun freqQueryTest() {
        redirectSystemIn(this.javaClass) {
            //            val scan = Scanner(System.`in`)
            val reader = BufferedReader(InputStreamReader(System.`in`))
            redirectSystemOut { expected ->
                while (true) {
                    val q = reader.readLine()!!.trim().toInt()

                    val queries = Array(q) { arrayOf(0, 0) }

                    var line: String
                    var idx: Int
                    for (i in 0 until q) {
                        line = reader.readLine()!!.trim()
                        idx = line.indexOf(' ', 1)
                        queries[i][0] = line.substring(0, idx).toInt()
                        queries[i][1] = line.substring(idx + 1).toInt()
                    }

                    val ans = freqQuery(queries)

                    println(ans.joinToString("\n"))

                    for (i in 0 until ans.size) {
                        expected.add(reader.readLine().trim())
                    }

                    val endLine = reader.readLine()
                    if (endLine != "=") {
                        break
                    }
                }
            }
        }
    }
}